package com.huiztech.kaoqinji;


import android.app.ActivityGroup;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

@SuppressWarnings("deprecation")
public class MainTabActivity extends ActivityGroup implements OnTabChangeListener {
    RadioGroup radiaGroup;
    LayoutParams lp;
    private LayoutInflater mInflater = null;
    private TabHost tabHost = null;
    LinearLayout visitLayou;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        mInflater = LayoutInflater.from(this);
        visitLayou = (LinearLayout) findViewById(R.id.main_footprint_visit_layout);
        tabHost = (TabHost) findViewById(R.id.mytabhost);
        tabHost.setup(this.getLocalActivityManager());
        Intent intent;

        intent = new Intent(this, RemarkListActivity.class);
        View tab1Spec = mInflater.inflate(R.layout.tab1_spec, null);
        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator(tab1Spec).setContent(intent));

        intent = new Intent(this, MainActivity.class);
        View tab2Spec = mInflater.inflate(R.layout.tab2_spec, null);
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator(tab2Spec).setContent(intent));

        tabHost.setCurrentTab(1);
        tabHost.setOnTabChangedListener(this);
        visitLayou.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainTabActivity.this, VisitActivity.class));

            }
        });
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN
                && event.getRepeatCount() == 0) {
            // 具体的操作代码
            InitDialog();
        }
        return super.dispatchKeyEvent(event);
    }

    private void InitDialog() {
        Dialog alertDialog = new AlertDialog.Builder(this).setMessage("您确定退出应用")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //                        SharedPreferences sharedata = getSharedPreferences("user", 0);
                        //                        sharedata.edit().clear().commit();
                        System.exit(0);

                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).create();
        alertDialog.show();
    }

    @Override
    public void onTabChanged(String tabId) {
        //        for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
        //            View vvv = tabHost.getTabWidget().getChildAt(i);
        //            if (tabHost.getCurrentTab() == i) {
        //                //选中后的背景
        //                vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.maintab_select_bg));
        //            } else {
        //                //非选择的背景
        //                vvv.setBackgroundDrawable(null);
        //            }
        //        }
    }
}